﻿using System;
using System.Data;
using System.Data.Sql;
using System.Windows.Forms;

namespace ContagemFieiras
{
    public partial class frmSql : Form
    {
        frmEntrada _entrada = new frmEntrada();
        public frmSql()
        {
            InitializeComponent();
        }

        private void frmSql_Load(object sender, EventArgs e)
        {
            cbDB.GotFocus += new EventHandler(cbDB_GotFocus);
            chkLocal.Checked = Properties.Settings.Default.LocalChecked;
            Application.DoEvents();
            GetSqlServers();
        }

        private void GetSqlServers()
        {
            DataTable sqlServersTable = SqlDataSourceEnumerator.Instance.GetDataSources();

            foreach (DataRow rowOfData in sqlServersTable.Rows)
            {
                string serverName = rowOfData["ServerName"].ToString();
                string instanceName = rowOfData["InstanceName"].ToString();
                if (!instanceName.Equals(string.Empty)) serverName = serverName + "\\" + instanceName;
                cbServers.Items.Add(serverName);
            }
        }

        protected void cbDB_GotFocus(object sender, EventArgs e)
        {
            try
            {
                if (this.chkLocal.Checked)
                {
                    Properties.Settings.Default.connStr = "Data Source=" + this.cbServers.SelectedItem.ToString() + ";Integrated Security=SSPI; Connection Timeout=7";
                }
                else if ((!string.IsNullOrEmpty(this.txtUser.Text)) && (!string.IsNullOrEmpty(this.txtPass.Text)))
                {
                    Properties.Settings.Default.connStr = "Data Source=" + this.cbServers.SelectedItem.ToString() + "; User ID=" + this.txtUser.Text + "; Password=" + this.txtPass.Text + ";Connection Timeout=7";
                }
                _entrada.Conn.ConnectionString = Properties.Settings.Default.connStr;
                GetDatabases();
            }
            catch
            {
                MessageBox.Show("Impossível ligar ao servidor de dados.", "Erro de Ligação", MessageBoxButtons.OK);
            }
        }

        protected void GetDatabases()
        {
            try
            {
                _entrada.Conn.Open();
                DataTable tblDatabases = _entrada.Conn.GetSchema("Databases");
                foreach (DataRow row in tblDatabases.Rows)
                    if (row["database_name"].ToString().Substring(0,2) == "AP") cbDB.Items.Add(row["database_name"].ToString());
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
            _entrada.Conn.Close();
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
            if (chkLocal.Checked)
                Properties.Settings.Default.connStr = "data source=" + cbServers.SelectedItem + "; Initial Catalog=" + cbDB.SelectedItem + "; integrated security=sspi; connection timeout=7";
            else if ((!string.IsNullOrEmpty(txtUser.Text)) && (!string.IsNullOrEmpty(txtPass.Text)))
                Properties.Settings.Default.connStr = "data source=" + cbServers.SelectedItem + "; Initial Catalog=" +
                    cbDB.SelectedItem + "; user id=" + txtUser.Text + "; password=" + txtPass.Text + ";connection timeout=7";

            _entrada.Conn.ConnectionString = Properties.Settings.Default.connStr;
            Properties.Settings.Default.Save();
            Close();
        }

        private void btnSair_Click(object sender, EventArgs e)
        { Application.Exit(); }

    }
}
